unit oFormPaths;

{
	created   5.4.2010
	author   	Miroslav Mintal
	purpose
}


interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls,
 Mathematic, Vector, RenderContext, View, Pedestrian, Obstacle, Path, uOSPRand, GL, GLu,
 Menus, Buttons, CheckLst, oProcessObjectConstants, oEntityTypeConstants,
 oProcessObjects, OProcessItem, oEditorManager, schodyFrm,
 oFrameScene, oFramePaths;

type
  TformPaths = class(TForm)
    FramePaths1: TFramePaths;
    TfEditor1: TfEditor;
    procedure fPaths1btnSaveClick(Sender: TObject);
    procedure fPaths1btnCancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    function getFrame: TFrame;
    { Private declarations }
  public
    procedure init;
    property frame: TFrame read getFrame;
    { Public declarations }
  end;

var
  formPaths: TformPaths;

implementation

{$R *.dfm}

procedure TformPaths.FormCreate(Sender: TObject);
begin
 inherited;
 init;
end;

procedure TformPaths.FormDestroy(Sender: TObject);
var I: Integer;
begin
  with TfEditor1 do begin
    RenderContext.Relase;
    RenderContext.Free;
    OrthogonalView.Free;
    for I := 0 to Pedestrians.Count -1 do begin
        TPedestrianEditor(Pedestrians[I]).Free;
    end;
    Pedestrians.Free;

    for I := 0 to Obstacles.Count -1 do begin
        TObstacle(Obstacles[I]).Free;
    end;
    Obstacles.Free;

    for I := 0 to LogObstacles.Count - 1 do begin
        TObstacle(LogObstacles[I]).Free;
    end;
    LogObstacles.Free;

    for I := 0 to Spawns.Count - 1 do begin
      TObstacle(Spawns[I]).Free;
    end;
    Spawns.Free;

    for I := 0 to Sinks.Count - 1 do begin
      TObstacle(Sinks[I]).Free;
    end;
    Sinks.Free;

    for I := 0 to PathPoints.Count - 1 do begin
      TObstacle(PathPoints[I]).Free;
    end;
    PathPoints.Free;


  end;

end;

procedure TformPaths.fPaths1btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TformPaths.fPaths1btnSaveClick(Sender: TObject);
begin
  FramePaths1.btnSaveClick(Sender);
  Close;
end;

function TformPaths.getFrame: TFrame;
begin
  Result := FramePaths1;
end;

procedure TformPaths.init;
var Pedestrian: TPedestrianEditor;
    I: Integer;
    pos : TVector;
begin
 with TfEditor1 do begin
    Manager := FramePaths1.Manager;
    pathName := FramePaths1.txtName;
    Application.OnIdle :=  IdleLoop;
    ViewWidth := LevelSize;
    RenderContext := TRenderContext.Create;
    PathsRBtn.Checked:= true;

    TransfBtn.Enabled:= false;
    PolyBtn.Enabled:= false;
    CircleBtn.Enabled:= false;
    SpawnBtn.Enabled:= false;
    SinkBtn.Enabled:= false;
    PathPointBtn.Enabled:= false;
    PathWayBtn.Enabled:= true;

    if not RenderContext.Init(RenderPanel.Handle, true) then Caption := 'RenderContext.Init Error';
//    OrthogonalView := TOrthogonalView.Create;
    pos := TVector.Create(LevelSize / 2, LevelSize / 2, 0);
    OrthogonalView := TOrthogonalView.Create(pos, ViewWidth, ViewWidth, 0);

    Pedestrians := TPedestriansList.Create;
    Obstacles := TObstaclesList.Create;
    LogObstacles:= TObstaclesList.Create;
    Spawns:= TObstaclesList.Create;
    Sinks:= TObstaclesList.Create;
    PathPoints:= TObstaclesList.Create;
    AllObstacles:= TObstaclesList.Create;
    Path:= TPath2.Create;

    PickedPedestrianIndex := -1;
    PickedObstacleIndex := -1;
    PickedSpawnIndex := -1;
    PickedSinkIndex := -1;
    PickedPathPointIndex:= -1;
    PickedPointIndex := -1;
    PickedPointIndex1 := -1;
    PickedPointIndex2 := -1;
    PickedPointIndex3 := -1;
    PolygonObstacle := nil;
    CircleObstacle := nil;
    R := 0.5; G := 0.5; B := 0.5;

    // nastavenie velkosti mriezky
    GridX := 1; GridY := 1;

    Start := false;
    // vytvorenie GL sceny
    InitGLScene;
    Randomize;
//    schodyFrm:= TSchodiskoFrm.Create(self);
  end;
end;

end.
